import java.io.*;

/*
 * Client Application to talk to the server
 * and send requests
 */
public class ClientApp {
	
	private static UDPClient client = null;
	/**
     * Accept input from user when program is first run
     * @param args[0] semantic option (1 or 2)
	 * @param args[1] freshness interval t
     */
	public static void main(String[] args) {
		String firstArg = args[0], secondArg = args[1];
		int semanticOption = 0;
		int freshnessInterval = 0;
		if (args.length > 0) {
			try {
				//Semantic option
				semanticOption = Integer.parseInt(args[0]);
				 switch(semanticOption) {
					case 1:
						System.out.println("\nAt Least Once Semantic");
						break;
					case 2:
						System.out.println("\nAt Most Once Semantic");
						break;
					default:
						System.err.println("\nNo such option exist.");
						System.exit(1);
				}
				//Freshness interval t
				freshnessInterval = Integer.parseInt(secondArg);
				System.out.println("Freshness interval t = " + freshnessInterval);
			} catch (NumberFormatException e) {
				System.err.println("Argument must be an integer");
				System.exit(1);
			}
			
			client = new UDPClient(semanticOption,freshnessInterval);
		
			//User menu
			boolean run = true;
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			while(run) {
				int requestOption = 0;
				System.out.println("\n\n==============================");
				System.out.println("1. Read");
				System.out.println("2. Write");
				System.out.println("3. Monitor");
				System.out.println("4. Create file");
				System.out.println("5. Append to End of File");
				System.out.println("6. Exit");
				System.out.print("Enter option: ");
				
				try {
					requestOption = Integer.parseInt(br.readLine());
					System.out.println("");
					switch(requestOption) {
						case 1:
							System.out.println("READ OPERATION");
							System.out.print("Enter file name: ");
							String filename = br.readLine();
							System.out.print("Enter offset: ");
							int offset = Integer.parseInt(br.readLine());
							System.out.print("Enter number of bytes to read: ");
							int numOfBytes = Integer.parseInt(br.readLine());
							System.out.println("Reading file contents...");
							System.out.println(client.read(filename,offset,numOfBytes));
							break;
						case 2:
							System.out.println("WRITE OPERATION");
							System.out.print("Enter file name: ");
							String filename2 = br.readLine();
							System.out.print("Enter offset: ");
							int offset2 = Integer.parseInt(br.readLine());
							System.out.print("Enter content: ");
							String content = br.readLine();
							System.out.println("Writing file contents...");
							System.out.println(client.write(filename2,offset2,content));
							break;
						case 3:
							System.out.println("MONITOR OPERATION");
							System.out.print("Enter file name: ");
							String filename3 = br.readLine();
							System.out.print("Enter time interval: ");
							int interval = Integer.parseInt(br.readLine());
							System.out.println("Monitoring file " + filename3 +"...");
							System.out.println(client.monitor(filename3,interval));	
							break;
						case 4:
							System.out.println("CREATE FILE OPERATION");
							System.out.print("Enter file name: ");
							String filename4 = br.readLine();
							System.out.println(client.createFile(filename4));
							break;
						case 5:
							System.out.println("APPEND OPERATION");
							System.out.print("Enter file name: ");
							String filename5 = br.readLine();
							System.out.print("Enter content: ");
							String content5 = br.readLine();
							System.out.println("Writing file contents...");
							System.out.println(client.append(filename5,content5));
							break;
						case 6:
							System.out.println("System exit");
							if(client.closeServer()) {
								run = false;
							} else {
								System.out.println("Server failed to close.");
							}
							break;
						default:
							System.err.println("\nNo such option exist.\n");
					}
				} catch (NumberFormatException ex) {
					System.err.println("\nNot a valid number.\n");
				} catch (IOException e) {
					System.err.println("\nAn IO error occured.\n");
				}
			}
        }
    }
}
